﻿using System;
using System.Collections.Generic;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viForum_ThreadView_ThreadView : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //set forum theme
        string themeCSS = WebRoots.GetModulesRoot + "viForum/Themes/" + BaseController.ForumTheme + "/viForum.css";
        WebPageController.AddCssLink(Page, themeCSS, true);

        Bind();    
    }

    private void Bind()
    {
        string forumidstr = Request.QueryString["ForumId"];
        if (!string.IsNullOrEmpty(forumidstr))
        {
            int forumid = 0;
            if (Int32.TryParse(forumidstr, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                if (forum != null)
                {
                    List<BaseThread> list = new List<BaseThread>();
                    int threadid = 0;
                    if (Int32.TryParse(Request.QueryString["ThreadId"], out threadid))
                    {
                        Thread thread = ThreadController.GetThread(threadid);
                        // SEO            
                        WebPageController.AddTitle(Page, thread.Title);

                        List<Reply> replies = ReplyController.GetReplies(threadid);
                        list.Add(thread);
                        foreach (Reply reply in replies)
                        {
                            list.Add(reply);
                        }
                        ThreadViewList.DataSource = list;
                        ThreadViewList.DataBind();
                    }
                }
            }
        }
    }

    public string GetThreadTitle()
    {
        int threadid = 0;
        if (Int32.TryParse(Request.QueryString["ThreadId"], out threadid))
        {
            Thread thread = ThreadController.GetThread(threadid);
            return thread.Title;
        }
        return null;
    }

    public int GetUserPostsCount(string UserName)
    {
        User user = UserController.GetUser(UserName);
        if (user != null)
            return user.PostsCount;
        else
            return 0;
    }

    protected void AddReply_Click(object sender, EventArgs e)
    {        
        int threadid = 0;
        if (Int32.TryParse(Request.QueryString["ThreadId"], out threadid))
        {
            Response.Redirect(WebRoots.GetWebRoot + BaseController.viForumUrlSeparator + "/Editor/AddReply/" + Request.QueryString["ForumId"] + "/" + Request.QueryString["ThreadId"] + ".aspx");
        }        
    }

    public bool IsAdmin()
    {
        if (System.Threading.Thread.CurrentPrincipal.IsInRole("admin"))
            return true;
        return false;
    }

    protected void ThreadViewList_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
        //int index = e.Item.ItemIndex;
        //int replyid = 0;
        //if (Int32.TryParse(e.CommandArgument.ToString(), out replyid))
        //{
        //    int ud = replyid;
        //}
        //BaseThread rr = (BaseThread)e.Item.DataItem;
        //if (e.CommandName == "delete")
        //{
        //    int threadid = 0;
        //    if (Int32.TryParse(e.CommandArgument.ToString(), out threadid))
        //    {
        //        Thread thread = ThreadController.GetThread(threadid);
        //        if (thread != null)
        //        {
        //            ThreadController.DeleteThread(thread.ThreadId);
        //            Bind();
        //        }
        //    }
        //}
        //if (e.CommandName == "edit")
        //{
        //    int threadid = 0;
        //    if (Int32.TryParse(e.CommandArgument.ToString(), out threadid))
        //    {
        //        Thread thread = ThreadController.GetThread(threadid);
        //        if (thread != null)
        //        {
        //            ThreadController.DeleteThread(thread.ThreadId);
        //            Bind();
        //        }
        //    }
        //}
    }
}
